<template>
  <div class="sku-container">
    <el-card>
      <div>
        <el-table
          v-loading.lock="tableLoading"
          stripe
          border
          class="sku-table"
          :data="skuList"
        >
          <el-table-column label="序号" width="100" type="index" />
          <el-table-column prop="skuName" label="名称" />
          <el-table-column prop="skuDesc" label="描述" />
          <el-table-column prop="prop" label="默认图片">
            <template slot-scope="scope">
              <el-image
                style="width: 100px; height: 100px"
                :src="scope.row.skuDefaultImg"
              />
            </template>
          </el-table-column>
          <el-table-column prop="weight" label="重量" width="80" />
          <el-table-column prop="price" label="价格" width="80" />
          <el-table-column prop="prop" label="操作" min-width="100">
            <template #default="{ row }">
              <template v-for="(item, index) in hintButtonList">
                <HintButton
                  v-if="hanldeButtonVisible(row, index)"
                  :key="index"
                  v-bind="item"
                  @click="onClick(index, row)"
                />
              </template>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :disabled="ispaginationDisable"
          class="sku-pagination"
          :current-page.sync="currentPage"
          :page-sizes="[5, 10, 15]"
          :page-size.sync="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
          @size-change="onGetSkuList"
          @current-change="onGetSkuList"
        />
      </div>
    </el-card>
    <el-drawer :visible.sync="isShowDrawer" :show-close="false" size="40%">
      <el-row :gutter="40">
        <el-col :span="8"><div class="left-col">名称</div></el-col>
        <el-col :span="16"
          ><div class="right-col">{{ skuInfo.skuName }}</div></el-col
        >
      </el-row>
      <el-row :gutter="40">
        <el-col :span="8"><div class="left-col">描述</div></el-col>
        <el-col :span="16"
          ><div class="right-col">{{ skuInfo.skuDesc }}</div></el-col
        >
      </el-row>
      <el-row :gutter="40">
        <el-col :span="8"><div class="left-col">价格</div></el-col>
        <el-col :span="16"
          ><div class="right-col">{{ skuInfo.price }}</div></el-col
        >
      </el-row>
      <el-row :gutter="40">
        <el-col :span="8"><div class="left-col">平台属性</div></el-col>
        <el-col :span="16"
          ><div class="right-col">
            <el-tag
              v-for="item in skuInfo.skuAttrValueList"
              :key="item.id"
              type="success"
              >{{ item.attrName }}</el-tag
            >
          </div></el-col
        >
      </el-row>
      <el-row :gutter="40">
        <el-col :span="8"><div class="left-col">商品图片</div></el-col>
        <el-col :span="16"
          ><div class="right-col">
            <el-carousel trigger="click" height="300px" style="width: 300px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item.id"
              >
                <el-image
                  :src="item.imgUrl"
                  style="width: 300px; height: 300px"
                />
              </el-carousel-item>
            </el-carousel></div
        ></el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
import pagenation from "@/mixin/pagenation";
import HintButton from "components/hintButton";
export default {
  name: "SkuManage",
  components: {
    HintButton,
  },
  mixins: [pagenation],
  props: {},
  data() {
    return {
      pageSize: 10,
      skuList: [],
      total: 0,
      tableLoading: false,
      isShowDrawer: false,
      skuInfo: {},
      hintButtonList: [
        {
          icon: "el-icon-top",
          type: "success",
          content: "上架",
          size: "mini",
          status: "up",
        },
        {
          icon: "el-icon-bottom",
          type: "info",
          content: "下架",
          size: "mini",
          status: "down",
        },
        {
          icon: "el-icon-edit",
          type: "primary",
          content: "查看Sku列表",
          size: "mini",
          status: "",
        },
        {
          icon: "el-icon-warning",
          type: "info",
          content: "查看sku信息",
          size: "mini",
          status: "",
        },
        {
          icon: "el-icon-delete",
          type: "danger",
          content: "删除sku",
          size: "mini",
          status: "",
        },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {
    this.onGetSkuList();
  },
  mounted() {},
  methods: {
    async onGetSkuList() {
      this.tableLoading = true;
      const { currentPage, pageSize } = this;
      const { data: res } = await this.$api.sku.getSkuList(
        currentPage,
        pageSize
      );
      this.skuList = res.records;
      this.total = res.total;
      this.tableLoading = false;
    },
    // 这里要注意一下,点击上架代表这个商品本身的状态是未上架的,所以要改变状态
    hanldeButtonVisible(row, index) {
      // 最后面3个按钮长期显示
      if (index !== 0 && index !== 1) {
        return true;
      }
      // 状态为上架且index === 0 显示
      if (index === 0 && row.isSale === 0) {
        return true;
      }
      // 状态为下架且index === 1 显示
      if (index === 1 && row.isSale === 1) {
        return true;
      }
      return false;
    },
    onClick(index, row) {
      const strategies = {
        0: async () => {
          this.tableLoading = true;
          const res = await this.$api.sku.putawaySku(row.id);
          if (res.code === 200) {
            console.log(res);
            this.$message.success("上架成功");
            this.onGetSkuList();
          } else {
            this.$message.success("操作失败");
          }
        },
        1: async () => {
          this.tableLoading = true;
          const res = await this.$api.sku.pulldownSku(row.id);
          if (res.code === 200) {
            this.$message({
              message: "下架成功",
              type: "success",
            });
            this.onGetSkuList();
          }
        },
        2: () => {},
        3: async () => {
          this.isShowDrawer = true;
          const { data: res } = await this.$api.sku.getSkuInfoById(row.id);
          this.skuInfo = res;
        },
        4: async () => {},
      };
      strategies[index]();
    },
  },
};
</script>

<style>
body {
  margin: 0;
}
</style>
<style scoped lang="scss">
.el-row {
  margin-bottom: 30px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-tag {
  margin-right: 10px;
  margin-bottom: 10px;
}
.sku-pagination {
  text-align: center;
}
.left-col {
  text-align: right;
  font-size: 18px;
  color: lightcoral;
  font-weight: 500;
}
.right-col {
  line-height: 23px;
}
</style>
